package com.atguigu.gmall.item.all.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.client.ListFeignClient;
import com.atguigu.gmall.model.list.SearchParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class ListController {

    //  http://list.gmall.com/list.html?category3Id=61&props=23:64G:机身内存
    //  http://list.gmall.com/list.html?keyword=小米手机
    //  远程调用
    @Autowired
    private ListFeignClient listFeignClient;

    @GetMapping("list.html " )
    public String listPage(SearchParam searchParam, Model model){
        //  需要存储 searchParam，trademarkParam，${propsParamList} ... trademarkList attrsList .... 属性是页面渲染的key！
        //  ${urlParam} 表示 用户点击哪个平台属性值时进行拼接的地址！
        //  https://list.jd.com/list.html?cat=9987%2C653%2C655&ev=1_122298

        Result<Map> result = listFeignClient.list( searchParam );
        //获取到的数据
        model.addAllAttributes( result.getData() );//一次性存储很多key！
        model.addAttribute( "searchParam",searchParam );
        //  获取到用户点击哪些平台属性值组成的url 地址！
        String urlParam = this.makeUrlParam(searchParam);
        model.addAttribute("urlParam",urlParam);
        //  品牌面包屑：
        String trademarkParam = this.makeTrademarkParam(searchParam.getTrademark());
        model.addAttribute("trademarkParam",trademarkParam);
        //  存储一个集合
        List<Map> propsParamList = this.makePropsParamList(searchParam.getProps());
        model.addAttribute("propsParamList",propsParamList);

//  设置排序规则：
        Map<String,Object> orderMap = this.orderMap(searchParam.getOrder());
        model.addAttribute("orderMap",orderMap);

        //返回视图名称
        return "list/index";

    }
//创建排序规则
    private Map<String, Object> orderMap(String order) {
        Map<String, Object> map = new HashMap<>();
        //  判断
        if (!StringUtils.isEmpty(order)){
            //  获取到排序规则！    order=2:desc  2:asc
            String[] split = order.split(":");
            if (split!=null && split.length==2){
                map.put("type",split[0]);
                map.put("sort",split[1]);
            }
        }else {
            map.put("type","hotScore");
            map.put("sort","desc");
        }
        return map;
    }

    //创建方法
    private List<Map> makePropsParamList(String[] props) {
        List<Map> mapList = new ArrayList<>();
        //  判断
        if (props != null && props.length > 0) {
            //  遍历这个数组
            for (String prop : props) {
                //  获取到的是单个字符串对象：  23:4G:运行内存
                String[] split = prop.split( ":" );
                if (split != null && split.length == 3) {
                    HashMap<String, Object> map = new HashMap<>();
                    map.put( "attrId", split[0] );
                    map.put( "attrValue", split[1] );
                    map.put( "attrName", split[2] );
                    mapList.add( map );
                }
            }
        }
        //  返回数据
        return mapList;
    }
    //  获取品牌的面包屑
    private String makeTrademarkParam(String trademark) {

        //  判断
        if (!StringUtils.isEmpty(trademark)){
            //  trademark = 1:小米 分割
            String[] split = trademark.split(":");
            if (split!=null && split.length==2){
                //  返回面包屑
                return "品牌："+split[1];
            }

        }
        return null;
    }

    //  制作url 参数地址
    private String makeUrlParam(SearchParam searchParam) {
        //  创建一个对象
        StringBuffer sb = new StringBuffer();

        //  需要知道按照哪个入口进行的检索：
        if (!StringUtils.isEmpty(searchParam.getKeyword())) {
            //  按照关键字检索
            sb.append("keyword=").append(searchParam.getKeyword());
        }

        if (!StringUtils.isEmpty(searchParam.getCategory3Id())) {
            //  按照分类检索
            sb.append("category3Id=").append(searchParam.getCategory3Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory2Id())) {
            //  按照分类检索
            sb.append("category2Id=").append(searchParam.getCategory2Id());
        }
        if (!StringUtils.isEmpty(searchParam.getCategory1Id())) {
            //  按照分类检索
            sb.append("category1Id=").append(searchParam.getCategory1Id());
        }

        //  按照平台属性值检索过滤！
        //  http://list.gmall.com/list.html?category3Id=61&props=23:64G:机身内存
        String[] props = searchParam.getProps();
        if (props!=null && props.length>0) {
            //  遍历
            for (String prop : props) {
                //  判断
                if (sb.length() > 0) {
                    sb.append( "&props=" ).append( prop );
                }
            }
        }
            //  通过品牌
            //  http://list.gmall.com/list.html?category3Id=61&props=23:64G:机身内存&trademark=1:小米
            String trademark = searchParam.getTrademark();
            if (!StringUtils.isEmpty(trademark)){
                //  判断
                if (sb.length()>0){
                    sb.append("&trademark=").append(trademark);
                }
            }
            //  返回数据
            return "list.html?" + sb.toString();
        }
    }